<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <!-- <p>{{ sex === 1 ? "男" : "女" }}</p> -->
      <p>{{ sex | fn | fn2 }}</p>
      <p>{{ timer | format }}</p>
      <p :title="timer | format">hello</p>
    </div>

    <script src="../Vue.js"></script>
    <script>
      // 过滤器的作用：文本格式化
      // 双大括号和v-bind都能使用过滤器
      // 可以串联
      new Vue({
        el: "#app",
        data: {
          sex: 1,
          timer: 1646636211986,
        },
        // 过滤器函数
        filters: {
          fn(val) {
            return val === 1 ? "男" : "女";
          },
          fn2(val) {
            return val === "男" ? "♂" : "♀";
          },
          format(val) {
            var date = new Date(val);
            var YY = date.getFullYear() + "-";
            var MM =
              (date.getMonth() + 1 < 10
                ? "0" + (date.getMonth() + 1)
                : date.getMonth() + 1) + "-";
            var DD =
              date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            var hh =
              (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) +
              ":";
            var mm =
              (date.getMinutes() < 10
                ? "0" + date.getMinutes()
                : date.getMinutes()) + ":";
            var ss =
              date.getSeconds() < 10
                ? "0" + date.getSeconds()
                : date.getSeconds();
            return YY + MM + DD + " " + hh + mm + ss;
          },
        },
      });
    </script>
  </body>
</html>
